package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/checkGroup")
@PreAuthorize("hasRole('ROLE_ADMIN')")
public class CheckGroupController {

    @Reference //RPC远程调用 CheckGroupService
    private CheckGroupService checkGroupService;

    /**
     * 分页条件查询检查组
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public Result queryPage(@RequestBody QueryPageBean queryPageBean) {

        Result result = checkGroupService.queryPage(queryPageBean);

        return result;
    }


    /**
     * 通过检查组ID查询检查组
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {

        Result result = checkGroupService.findById(id);

        return result;
    }

    /**
     * 通过检查组ID查询该检查组下包含多少个检查项id
     *
     * @param id
     * @return
     */
    @RequestMapping("/findCheckItemIdsByCheckGroup/{id}")
    private Result findCheckItemIdsByCheckGroup(@PathVariable Integer id) {

        Result result = checkGroupService.findCheckItemIdsByCheckGroup(id);
        return result;
    }

    /**
     * 通过检查组ID删除检查组 并且删除检查组和检查项的关系
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    private Result delete(@PathVariable Integer id) {

        Result result = checkGroupService.deleteCheckGroup(id);
        return result;
    }

    /**
     * 添加检查组 并且向中间表中添加与检查项的关系
     *
     * @param checkGroup
     * @param checkItemIds
     * @return
     */
    @RequestMapping("/add")
    private Result add(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds) {

        Result result = checkGroupService.addCheckGroup(checkGroup, checkItemIds);
        return result;
    }

    /**
     * 编辑检查组
     *
     * @param checkGroup
     * @param checkItemIds
     * @return
     */
    @RequestMapping("/edit")
    private Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds) {

        Result result = checkGroupService.editCheckGroup(checkGroup, checkItemIds);
        return result;
    }


    /**
     * 查询所有
     * @return
     */
    @RequestMapping("/findAll")
    private Result findAll() {
        Result result = checkGroupService.findAll();
        return result;
    }


    /**
     * 通过检查套餐的id查询检查组id
     * @param id
     * @return
     */
    @RequestMapping("/getCheckgroupBySetmeal/{id}")
    private Result getCheckGroupBySetmeal(@PathVariable Integer id) {

        Result result = checkGroupService.findCheckGroupIdsBySetmealId(id);
        return result;
    }
}
